package com.xt.LibaryDeskManager.servlet;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xt.LibaryDeskManager.dto.SeatDto;
import com.xt.LibaryDeskManager.dto.UserMessage;
import com.xt.LibaryDeskManager.service.SeatService;
import com.xt.LibaryDeskManager.utils.StrUtils;
import com.xt.LibaryDeskManager.vo.SeatVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/seat")
public class SeatController {
    @Autowired
    private SeatService seatService;

    /**
     * 获得所有座位信息
     * @param page
     * @param limit
     * @throws ServletException
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping("/selectAll")
    public Map selectAll(Integer page, Integer limit, String floor, String room) throws Exception {
        Page page1= PageHelper.startPage(page,limit,true);
       List<SeatDto> list=seatService.selectAll(floor+room);
       return StrUtils.creatMap(list,page1.getTotal());
    }

    /**
     * 获得指定教师管理的座位信息
     * @throws ServletException
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping("/selectSeatByTeacher")
    public Map<String, Object> selectSeatByTeacher(Integer page, Integer limit, String floor, String room, HttpSession session) throws Exception {
        int teacherNo= Integer.parseInt(((UserMessage)session.getAttribute("user")).getId());
        Page page1= PageHelper.startPage(page,limit,true);
        List<SeatDto> list = seatService.selectAll(teacherNo,floor+room);
        return StrUtils.creatMap(list,page1.getTotal());
    }

    /**
     * 学生获得所有座位信息
     * @throws ServletException
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping("/selectAllSeat")
    public Map selectAllSeat(Integer page,Integer limit,String floor, String room) throws Exception {
        Page page1= PageHelper.startPage(page,limit,true);
        List<SeatDto> list=seatService.selectAll(floor+room);
        return StrUtils.creatMap(list,page1.getTotal());
    }
    /**
     * 获得座位数量
     * @throws ServletException
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping("/getCount")
    public Map getCount() throws Exception {
        int count=seatService.getCount();
        return StrUtils.creatMessage("count",count);
    }
    /**
     * 更新座位信息
     */
    @ResponseBody
    @RequestMapping("/updateSeat")
    public Map updateSeat(SeatVo seatVo)throws Exception{
        int code= seatService.updateSeat(seatVo);
        return StrUtils.creatMessage("code",code);
    }

    /**
     * 查找房间可添加座位
     * @throws ServletException
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping("/getAvailableSeatNumber")
    public Map getAvailableSeatNumber(String floor,String room)throws Exception{
        int code=0;
        String no=seatService.getAvailableSeatNumber(floor+room);
        if ("100".equals(no)){
            code=0;
        }else{
            code=1;
        }
        Map map=new HashMap();
        map.put("code",code);
        map.put("no",no);
        return map;
    }
    /**
     * 添加座位
     * @param req
     * @throws ServletException
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping("/addSeat")
    public Map addSeat(HttpServletRequest req)throws Exception{
        SeatVo seatVo=new SeatVo(req.getParameter("floor")+req.getParameter("room")+req.getParameter("no"),Integer.parseInt(req.getParameter("color")),0,0,req.getParameter("teacherNo"));
        int code=seatService.addSeat(seatVo);
        return StrUtils.creatMessage("code",code);
    }

    /**
     * 删除座位
     * @throws ServletException
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping("/deleteSeat")
    public Map<String, Object> deleteSeat(String seatNo)throws Exception{
        int code=seatService.deleteSeat(seatNo);
        return StrUtils.creatMessage("code",code);
    }

    @ResponseBody
    @RequestMapping("/adminOperating")
    public Map adminOperating()throws Exception{
        int code=seatService.updateSeat();
        return StrUtils.creatMessage("code",code);
    }
    @ResponseBody
    @RequestMapping("/getNum")
    public Map getNum()throws Exception{
       List<SeatDto> list=seatService.selectAll("00");
       int num=0;
       int count=list.size()*3;
        for (SeatDto seatDto : list) {
            if (seatDto.getStatue()!=0){
                num+=seatDto.getStatue();
            }
        }
        Map map=new HashMap();
        map.put("num",num);
        map.put("count",count);
        return map;
    }
}
